   // ****************************************************** 
   //
   // Copyright 2011 OpenAjax Alliance
   //
   // Licensed under the Apache License, Version 2.0 (the "License");
   // you may not use this file except in compliance with the License.
   // You may obtain a copy of the License at
   //
   //    http://www.apache.org/licenses/LICENSE-2.0
   //
   // Unless required by applicable law or agreed to in writing, software
   // distributed under the License is distributed on an "AS IS" BASIS,
   // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   // See the License for the specific language governing permissions and
   // limitations under the License.
   //
   // ****************************************************** 
   
OAA_CI.tables = OAA_CI.tables || {}; 

/** ================================================================
*
* Tables Module initialization
*
* =============================================================== */

OAA_CI.current_table_element = null;


/** ================================================================
*
* onLoad
*
* @desc Initializes tables module
*
* @return nothing
*
* =============================================================== */

OAA_CI.tables.onLoad = function(oaa_cache_inspector) {
  
  OAA_CI.tables_nls_strings = document.getElementById("ID_STRINGBUNDLE_LANDMARKS");        

  OpenAjax.a11y.console("Loading table strings: " + OAA_CI.tables_nls_strings);
  
  OAA_CI.tables.onShowTablesCommand();
  
};

/** ================================================================
*
* onShowTablesCommand
*
* @desc Evaluates the document and populates the user interface controls
*       with table information
*
* @return nothing
*
* =============================================================== */

OAA_CI.tables.onShowTablesCommand = function(e) {

  function addTableChildren(xul_treechildren, table_elements) {
   
    var j;
  
    var table_elements_len = table_elements.length;
    
    for (j=0; j<table_elements_len; j++) {
    
      te = table_elements[j];
    
      var xul_treeitem = OAA_CI.createXULElement('treeitem', []);
      
      OAA_CI.addTreeRow(xul_treeitem, [te.document_order, te.is_data_table, te.row, te.column, te.caption, te.summary], te.cache_id );
      
      if (te.child_tables.length) {
      
         xul_treeitem.setAttribute("container", "true");
         xul_treeitem.setAttribute("open", "true");

         var xul_sub_treechildren = OAA_CI.createXULElement('treechildren', []);

         addTableChildren(xul_sub_treechildren, te.child_tables);
         
         xul_treeitem.appendChild(xul_sub_treechildren);
         
      }
      
      xul_treechildren.appendChild(xul_treeitem);
      
    } // loop
  
  } 

  var i;
  var te;

  OAA_CI.evaluateDocument();   
  
  var child_tables = OAA_CI.evaluation.dom_cache.tables_cache.child_tables;

  if (child_tables === null) return;
  
  var child_tables_len = child_tables.length;
 
  var start_time = new Date().getTime();
  
  var xul_treechildren = document.getElementById('ID_TREECHILDREN_TABLES');

  OAA_CI.removeChildNodes(xul_treechildren);

  if (child_tables_len) {
    addTableChildren(xul_treechildren, child_tables);
  }
  else {
    var xul_treeitem = OAA_CI.createXULElement('treeitem', []);
    OAA_CI.addTreeRow(xul_treeitem, ["none","","","","",""], "none" );
    xul_treechildren.appendChild(xul_treeitem);
  }
    
  OAA_CI.updateXULTime(start_time);
    
  
};  


/** ================================================================
*
* onTableElementItemsSelectChanged
*
* @desc Updates properties tappanel with the information from the currently selected 
*       item in the TableCache
*
* @return nothing
*
* =============================================================== */

OAA_CI.tables.onTableElementItemsSelectChanged = function (event) {

  function addTableRowCells(xul_treechildren, table_row_cells) {
  
    var xul_treeitem;
    var cell;
    var last_cell = null;
    var table_row_cells_len = table_row_cells.length;
    
    for (var i=0; i<table_row_cells_len; i++) {
    
      cell = table_row_cells[i];
    
      if (cell && (cell != last_cell)) {
        xul_treeitem = OAA_CI.createXULElement('treeitem', []);     
        OAA_CI.addTreeRow(xul_treeitem, [cell.dom_element.tag_name, cell.dom_element.id, cell.headers,cell.row_span, cell.col_span], cell.cache_id );
        xul_treechildren.appendChild(xul_treeitem);
      }  
       
      last_cell = cell;
    
    } // loop
  
  } // end function

  var i;
  var te;
  var col_id;
  var cache_id;
  var rows;
  
  var table_cell_rows;
  var xul_treechildren;
  var xul_sub_treechildren;
  
  var index = event.target.currentIndex;
  var tables_cache = OAA_CI.evaluation.dom_cache.tables_cache;  
  var xul_tree = document.getElementById('ID_TREE_TABLES');
  
  if (xul_tree) {
    col_id   = xul_tree.columns.getFirstColumn();
    cache_id = xul_tree.view.getCellValue(index, col_id);

    te = tables_cache.getTableElementByCacheId(cache_id);
    
    if (te) {
    
      OAA_CI.current_table_element = te;
    
      OAA_CI.showCacheItemProperties(te);
      
      rows = te.row;

      OpenAjax.a11y.console("  Table " + te.document_order + " rows: " + rows ); 
      
      xul_treechildren = document.getElementById('ID_TREECHILDREN_TABLE_CELLS');
  
      OAA_CI.removeChildNodes(xul_treechildren);
  
      if (rows) {
        for (i = 0; i<=rows; i++) {
        
          xul_treeitem = OAA_CI.createXULElement('treeitem', []);
      
          OAA_CI.addTreeRow(xul_treeitem, ["row " + i, "", "", "", ""], "none" );
      
          xul_treeitem.setAttribute("container", "true");
          xul_treeitem.setAttribute("open", "true");

          xul_sub_treechildren = OAA_CI.createXULElement('treechildren', []);
    
          addTableRowCells(xul_sub_treechildren, te.cells[i]);

          xul_treeitem.appendChild(xul_sub_treechildren);
 
          xul_treechildren.appendChild(xul_treeitem);
        }  
      }
      else {
        xul_treeitem = OAA_CI.createXULElement('treeitem', []);
        OAA_CI.addTreeRow(xul_treeitem, ["none","","","",""], "none" );
        xul_treechildren.appendChild(xul_treeitem);
      } 
    }
  }    
};


/** ================================================================
*
* onTableCellElementItemsSelectChanged
*
* @desc Updates properties tappanel with the information from the currently selected 
*       item in the TableCellElement
*
* @return nothing
*
* =============================================================== */

OAA_CI.tables.onTableCellElementItemsSelectChanged = function (event) {

  var cell;
  var col_id;
  var cache_id;
  
  var index = event.target.currentIndex;
  var xul_tree = document.getElementById('ID_TREE_TABLE_CELLS');
  
  if (xul_tree && OAA_CI.current_table_element) {
    col_id   = xul_tree.columns.getFirstColumn();
    
    if (col_id) {
      cache_id = xul_tree.view.getCellValue(index, col_id);

      OpenAjax.a11y.console("Table cell cache id: " + cache_id );

      cell = OAA_CI.current_table_element.getTableCellElementByCacheId(cache_id);

      OpenAjax.a11y.console("    " + cell );

      if (cell) {
        var xul_textbox_header = document.getElementById('ID_TABLE_CELL_HEADER');
        xul_textbox_header.value = cell.header;

        var xul_textbox_content = document.getElementById('ID_TABLE_CELL_CONTENT');
        xul_textbox_content.value = cell.dom_element.getText();

        OAA_CI.showCacheItemProperties(cell);
      }
    }  
  }    
};
